﻿#include<Windows.h>
#include"resource.h"
const char g_szClassName[]="myWindowClass";
HWND g_Result=NULL;
POINT ptStart;
POINT ptEnd;

typedef struct tagShape
{
	RECT rect;
	UINT shapeID;
} Shape;

const UINT SHAPE_COUNT = 5;
Shape Shapes[SHAPE_COUNT]; 
UINT nCurrentIndex = 0;

bool isRubberBand=false;
void Draw(HWND hwnd)
{
	HPEN hPen,hOldPen;
			HBRUSH hBrush,hOldBrush;
			HDC hdc;
			hdc=GetDC(hwnd);
			SetROP2(hdc, R2_NOT);
			hPen=CreatePen(PS_SOLID,3,RGB(255,0,0));
			hOldPen=(HPEN)SelectObject(hdc,hPen);
			hBrush=CreateSolidBrush(RGB(0,255,0));
			hOldBrush=(HBRUSH)SelectObject(hdc,hBrush);

			Ellipse(hdc,ptStart.x,ptStart.y,ptEnd.x,ptEnd.y);

			SelectObject(hdc,hOldBrush);
			SelectObject(hdc,hOldPen);
			DeleteObject(hBrush);
			DeleteObject(hPen);
			ReleaseDC(hwnd,hdc);
}
LRESULT OnLButtonDown (HWND hWnd, UINT nCtrl, UINT x, UINT y)
{
			//C: Set the start point.
	ptStart.x = x;
	ptStart.y = y;
			//C: Set the rubberband mode to true.
	isRubberBand = true;
			//C: Set the current point to the start point as well.
	ptEnd.x = x;
	ptEnd.y = y;
			//C: Draw the initial rubber band.
	Draw(hWnd);
			//C: Return.
	return 0;	
}
LRESULT OnMouseMove(HWND hwnd,UINT nCtrl, UINT x,UINT y)
{
	if(!isRubberBand)
		return 0;
	Draw(hwnd);
	ptEnd.x=x;
	ptEnd.y=y;
	Draw(hwnd);
	return 0;
}
LRESULT OnLButtonUp   (HWND hWnd, UINT nCtrl, UINT x, UINT y)
{
			//C: If the current mode is not rubber band, then exit.
	if (!isRubberBand)
	{
		return 0;
	}
			//C: Undo the last rectangle shape that was drawn.
	Draw(hWnd);
			//C: Update the dimensions of the current shape.
			//C: Get the current shape mode.
	Shapes[nCurrentIndex].rect.left		= ptStart.x;
	Shapes[nCurrentIndex].rect.top		= ptStart.y;
	Shapes[nCurrentIndex].rect.right	= ptEnd.x;
	Shapes[nCurrentIndex].rect.bottom	= ptEnd.y;
			//C: Increment the index.
	nCurrentIndex = (++nCurrentIndex)%SHAPE_COUNT;
			//C: Disable the rubberband mode.
	isRubberBand = false;
			//C: Update the window.
	InvalidateRect(hWnd, NULL, TRUE);
	UpdateWindow(hWnd);
			//C: Return with success.
	return 0;		
}
BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
	switch (msg)
	{
	case WM_INITDIALOG:
		return FALSE;
	case WM_COMMAND:
		switch (LOWORD(wParam))
		{
		case IDOK:
			EndDialog(hwnd,IDOK);
			break;
		}
		break;
	case WM_CLOSE:
		EndDialog(hwnd,0);
		break;
	default:
		return FALSE;
	}
	return TRUE;
}
HBITMAP hBitmap;
LRESULT OnPaint       (HWND hWnd)
{
	PAINTSTRUCT ps;
	HDC			hdc;
	hdc = ::BeginPaint(hWnd, &ps);
	HPEN hPen,hOldPen;
	HBRUSH hBrush,hOldBrush;		
	UINT index;
	for (index = 0; index < SHAPE_COUNT; index++)
	{
		
			hPen=CreatePen(PS_SOLID,3,RGB(255,0,0));
			hOldPen=(HPEN)SelectObject(hdc,hPen);
			hBrush=CreateSolidBrush(RGB(0,255,0));
			hOldBrush=(HBRUSH)SelectObject(hdc,hBrush);
			::Ellipse	(	
						hdc, 
						Shapes[index].rect.left, 
						Shapes[index].rect.top, 
						Shapes[index].rect.right,
						Shapes[index].rect.bottom
						);
			SelectObject(hdc,hOldBrush);
			SelectObject(hdc,hOldPen);
			DeleteObject(hBrush);
			DeleteObject(hPen);
		
	}

	::EndPaint(hWnd, &ps);

	return 0;	
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
	switch(msg)
	{
	case WM_CREATE:
		{
			HICON hIcon,hIconSm;
			HMENU hMenu,hSubMenu;

			hMenu=CreateMenu();

			hSubMenu=CreatePopupMenu();
			AppendMenu(hSubMenu,MF_STRING,ID_FILE_EXIT,"&Exit");
			AppendMenu(hMenu,MF_STRING|MF_POPUP,(UINT)hSubMenu,"&File");

			hSubMenu=CreatePopupMenu();
			AppendMenu(hSubMenu,MF_STRING,ID_HELP_ABOUT,"&Show");
			AppendMenu(hSubMenu,MF_STRING,ID_HELP_INFO,"&Hide");
			AppendMenu(hMenu,MF_STRING|MF_POPUP,(UINT)hSubMenu,"&Help");

			SetMenu(hwnd,hMenu);
			hIcon=(HICON)LoadImage(NULL,"my_icon.ico",IMAGE_ICON,32,32,LR_LOADFROMFILE);
			if(hIcon)
				SendMessage(hwnd,WM_SETICON,ICON_BIG,(LPARAM)hIcon);
			else
				MessageBox(hwnd,"Failed!","Sign hIcon",MB_OK|MB_ICONERROR);
			hIconSm=(HICON)LoadImage(NULL,"my_icon.ico",IMAGE_ICON,16,16,LR_LOADFROMFILE);
			if(hIconSm)
				SendMessage(hwnd,WM_SETICON,ICON_SMALL,(LPARAM)hIconSm);
			else
				MessageBox(hwnd,"Failed!","Sign hIcon",MB_OK|MB_ICONERROR);
			g_Result=CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_RESULT),hwnd,DlgProc);
			if(g_Result)
				ShowWindow(g_Result,SW_HIDE);
			else
				MessageBox(hwnd,"Failed","Dialog",MB_OK|MB_ICONERROR);
			
    		hBitmap = (HBITMAP)LoadImage(NULL, "ball.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
		}
		break;
		case WM_PAINT:
		{
			return OnPaint(hwnd);
		}
		break;
	//case WM_PAINT:
	//	{
	//		HDC hdc;
	//		HPEN hPen,hPenOld;
	//		PAINTSTRUCT ps;
	//		POINT lpPoints[4];
	//		lpPoints[0].x=460;
	//		lpPoints[0].y=10;
	//		lpPoints[1].x=500;
	//		lpPoints[1].y=110;
	//		lpPoints[2].x=600;
	//		lpPoints[2].y=60;
	//		lpPoints[3].x=730;
	//		lpPoints[3].y=60;
	//		hdc=BeginPaint(hwnd,&ps);
	//		COLORREF color;
	//		HBRUSH hBrush,hOldBrush;
	//		color=RGB(255,0,0);
	//		hPen=CreatePen(PS_SOLID,3,color);
	//		hPenOld=(HPEN)SelectObject(hdc,hPen);
	//		hBrush=(HBRUSH)CreateSolidBrush(RGB(255 ,130 ,71));
	//		hOldBrush=(HBRUSH)SelectObject(hdc,hBrush);
	//
	//		Ellipse(hdc,10,10,100,100);
	//		hBrush=(HBRUSH)CreateSolidBrush(RGB(0,255,0));
	//		hOldBrush=(HBRUSH)SelectObject(hdc,hBrush);
	//		Rectangle(hdc, 110, 10, 200, 100);
	//		hBrush=(HBRUSH)CreateSolidBrush(RGB(0,0,255));
	//		hOldBrush=(HBRUSH)SelectObject(hdc,hBrush);
	//		Rectangle(hdc, 210, 10, 350, 100);
	//		MoveToEx(hdc,360,10,NULL);
	//		LineTo(hdc,450,100);
	//		hBrush=(HBRUSH)CreateSolidBrush(RGB(255 ,246 ,143));
	//		hOldBrush=(HBRUSH)SelectObject(hdc,hBrush);
	//		Polygon(hdc,lpPoints,4);
	//		//Polyline(hdc,lpPoints,4);
	//		
	//		SelectObject(hdc,hOldBrush);
	//		DeleteObject(hBrush);
	//		ReleaseDC(hwnd,hdc);
	//			EndPaint(hwnd,&ps);
	//	}
	//	break;
			/*HDC hdc;
			PAINTSTRUCT ps;
			HPEN hPenOld,hLinePen;
			hdc=BeginPaint(hwnd,&ps);
			COLORREF qLineColor;
			qLineColor=RGB(250,0,0);
			hLinePen=CreatePen(PS_SOLID,3,qLineColor);
			hPenOld=(HPEN)SelectObject(hdc,hLinePen);
			MoveToEx(hdc,0,100,NULL);
			LineTo(hdc,150,100);
			SelectObject(hdc,hPenOld);
			DeleteObject(hLinePen);

			HPEN hEllipsePen;
			COLORREF qEllipsePen;
			qEllipsePen=RGB(0,0,255);
			hEllipsePen=CreatePen(PS_SOLID,3,qEllipsePen);
			hPenOld=(HPEN)SelectObject(hdc,hEllipsePen);
			Arc(hdc,0,0,150,100,0,0,0,0);
			SelectObject(hdc,hPenOld);
			DeleteObject(hEllipsePen);
*/
		
		/*{
    	PAINTSTRUCT 	ps;
    	HDC 			hdc;
    	BITMAP 			bitmap;
    	HDC 			hdcMem;
        HGDIOBJ 		oldBitmap;

    	hdc = BeginPaint(hwnd, &ps);

    	hdcMem = CreateCompatibleDC(hdc);
        oldBitmap = SelectObject(hdcMem, hBitmap);

        GetObject(hBitmap, sizeof(bitmap), &bitmap);
        BitBlt(hdc, 0, 0, bitmap.bmWidth, bitmap.bmHeight, hdcMem, 0, 0, SRCCOPY);

        SelectObject(hdcMem, oldBitmap);
        DeleteDC(hdcMem);

    	EndPaint(hwnd, &ps);
		}*/
    	
	case WM_COMMAND:
		switch(LOWORD(wParam))
		{
		case ID_FILE_EXIT:
			SendMessage(hwnd,WM_CLOSE,0,0);
			break;
		case ID_HELP_ABOUT:
			ShowWindow(g_Result,SW_SHOW);
			break;
		case ID_HELP_INFO:
			ShowWindow(g_Result,SW_HIDE);
			break;
		}
		break;
	/*case WM_LBUTTONDOWN:
		{
			char szFileName[MAX_PATH];
			HINSTANCE hInstance=GetModuleHandle(NULL);
			GetModuleFileName(hInstance,szFileName,MAX_PATH);
			MessageBox(hwnd,szFileName,"This program is:",MB_OK|MB_ICONINFORMATION);
		}
		break;*/
	case WM_LBUTTONDOWN:
		{
			return OnLButtonDown(hwnd, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
		}
		break;
	case WM_MOUSEMOVE:
		{
			return OnMouseMove(hwnd, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
		}
		break;
	case WM_LBUTTONUP:
		{
			return OnLButtonUp(hwnd, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
		}
		break;
	case WM_CLOSE:
		DestroyWindow(hwnd);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hwnd,msg,wParam,lParam);
	}
	return 0;
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdLine)
{
	WNDCLASSEX wc;
	wc.cbClsExtra=0;
	wc.cbSize=sizeof(WNDCLASSEX);
	wc.cbWndExtra=0;
	wc.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
	wc.hCursor=LoadCursor(NULL,IDC_ARROW);
	wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
	wc.hIconSm=LoadIcon(NULL,IDI_APPLICATION);
	wc.hInstance=hInstance;
	wc.lpfnWndProc=WndProc;
	wc.lpszClassName=g_szClassName;
	wc.lpszMenuName=NULL;
	wc.style=0;
	if(!RegisterClassEx(&wc))
	{
		MessageBox(NULL,"Failed!","Sign Class",MB_OK|MB_ICONEXCLAMATION);
		return 0;
	}
	HWND hwnd;
	hwnd=CreateWindowEx(WS_EX_CLIENTEDGE,g_szClassName,"Title",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,1000,500,NULL,NULL,hInstance,NULL);
	if(!hwnd)
	{
		MessageBox(NULL,"Failed!","Create Windows",MB_OK|MB_ICONEXCLAMATION);
		return 0;
	}
	ShowWindow(hwnd,nCmdLine);
	UpdateWindow(hwnd);
	MSG msg;
	while(GetMessage(&msg,NULL,0,0)>0)
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return msg.wParam;
}